package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.exceptions.*;

public class BankApplication {
	
	public static void main(String args[]) {
		Bank bank = BankService.createBank(new ClientRegistrationListener[] {new ClientRegistrationListener() {    // That is here just for test
		    public void onClientAdded(Client client) {
		        System.out.println("Listener from outside: " + client.getClientSalutation()+ "  registred");
		    }
		}});
		addSampleClients(bank);
		BankService.printClients(bank);
		modifyBank(bank);
		System.out.println("Bank is modified");
		BankService.printClients(bank);
	}

    
	private static void modifyBank(Bank bank) {
		for (Client cli : BankService.getClients(bank)) {
			if (Math.random() < 0.5) {
				cli.getAccount().deposit((int) (10000 * Math.random()));				
			} else {
			    try {
    				cli.getAccount().withdraw((int) (10000 * Math.random()));
    			} 
    			catch (OverDraftLimitExceededException e) {
    			    System.err.println("Error! "+cli.getClientSalutation()+" is to greedy while using overdraft! Wanted="+e.getAmountWanted()+", MaxAllowed="+e.getAmountAllowed()+" balance="+e.getAccount().getBalance());
    			}    			
    			catch (NotEnoughFundsException e) {
    			    System.err.println("Error! "+cli.getClientSalutation()+" is to greedy! Wanted="+e.getAmountWanted());
    			}
			}
		}		
	}
		
	public static Client createSampleClient(String name,Gender gender) {
		Client client = new Client(name,gender);
		if (Math.random() < 0.5) {
			client.setAccount(new SavingsAccount((int) (Math.random() * 10000)));			
		} else {
			client.setAccount(new CheckingAccount((int) (Math.random() * 10000), (int) (Math.random() * 10000)));
		}
		return client;
	}
	
	public static void addSampleClients(Bank bank) {
		BankService.addClient(bank,createSampleClient("Mary", Gender.FEMALE));
		BankService.addClient(bank,createSampleClient("Johny Doe", Gender.MALE));
		BankService.addClient(bank,createSampleClient("Scrooge McDuck", Gender.MALE));
		BankService.addClient(bank,createSampleClient("Jimmy", Gender.MALE));
		BankService.addClient(bank,createSampleClient("Billy", Gender.MALE));
	}
	
}
